{% extends "base.html" %}
{% load rollup accounts_tags %}

{% block title %}{{ block.super }} - Summary{% endblock %}

{% block extrahead %}
<link href="/media/css/accounts.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(function() {
        $(".account-group h3").click(function (e) {
            panel = $(this).parent().find("table")
            panel.stop()
            if (panel.is(":visible")) {
                panel.fadeOut("slow")
            } else {
                panel.fadeIn("slow")
            }
        })
    })
</script>
{% endblock %}

{% block breadcrumbs %}
<a href="{% url accounts-summary %}">Summary</a>
{% endblock %}

{% block content %}
<div id="accounts">

    <div>
        <table>
            <thead>
                <tr>
                    <th class="header" colspan="2">YTD Summary</th>
                </tr>
            </thead>
            <tbody class="highlight">
                <tr class="even">
                    <th>Expenses</th>
                    <td class="money negative">{{ expenses|yearly_balance|rollup:"yearly"|floatformat:0 }}</td>
                </tr>
                <tr class="odd">
                    <th>Revenue</th>
                    <td class="money positive">{{ revenues|yearly_balance|rollup:"yearly"|floatformat:0 }}</td>
                </tr>
                <tr class="even">
                    <th>Total</th>
                    <td class="money total">{% rollup_total "%.0f" "yearly" %}</td>
                </tr>
            </tbody>
            <thead><tr><th class="header" colspan="2">MTD Summary</th></tr></thead>

            <tbody class="highlight">
                <tr class="even">
                    <th>Expenses</th>
                    <td class="money negative">{{ expenses|monthly_balance|rollup:"monthly"|floatformat:0 }}</td>
                </tr>
                <tr class="odd">
                    <th>Revenue</th>
                    <td class="money positive">{{ revenues|monthly_balance|rollup:"monthly"|floatformat:0 }}</td>
                </tr>
                <tr class="even">
                    <th>Total</th>
                    <td class="money total">{% rollup_total "%.0f" "monthly" %}</td>
                </tr>
            </tbody>

            {% for type,accounts in oe.iteritems %}

            {% if accounts.count %}
            <thead>
                <tr class="header">
                    <th>{{ type }}</th>
                </tr>
                <tr>
                    <th>Account</th>
                    <th>Balance</th>
                </tr>
            </thead>
            {% for account in accounts %}

            <tr class="{% cycle 'even' 'odd' %}">

                <td class="{{ account.type|lower }}">
                    <a href="{% url accounts-ledger account.pk %}">{{ account.name }}</a>
                </td>
                <td class="money {{ account.has_normal_balance|yesno:'positive,negative' }}">
                    {{ account.normal_balance|rollup:"monthly"|default:""|floatformat:0 }}
                </td>
            </tr>
            {% endfor %}
            {% else %}
            {% ifequal type "Equity" %}
            <tr>
                <td colspan="2">You need to create an Equity account</td>
            </tr>
            {% endifequal %}
            {% endif %}
            {% endfor %}

            <tr class="total">
                <td class="right">Total:</td>
                <td class="money total">{% rollup_total "%.0f" "monthly" %}</td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: right;">
                    <a href="/admin/accounts/">Administer Accounts</a> |
                    <a href="/admin/accounts/entry/">View Journal</a>
                </td>
            </tr>

            <thead>
                <tr class="header">
                    <th>Error Check</th>
                </tr>
            </thead>
            <tr>
                <td colspan="2">Assets = Claims</td>
            </tr>
            <tr>
                <td colspan="2">{{ asset_total }} = {{ claims_total }}</td>
            </tr>
            <tr>
                <td colspan="2">{{ asset_total|add:claims_total }}</td>
            </tr>
        </table>
    </div>
    <div>
        <table>
            <thead>
                <tr class="header">
                    <th colspan="2">Assets</th>
                </tr>
                <tr>
                    <th>Account</th>
                    <th>Total</th>
                </tr>
            </thead>
            {% for account in assets %}
            <tr class="{% cycle 'even' 'odd' %}">
                <td class="{{ account.type|lower }}">
                    <a href="{% url accounts-ledger account.pk %}">{{ account.name }}</a>
                </td>
                <td class="money">
                    {% with account|monthly_balance as balance %}
                    <span style="font-size: 8pt;">{% if balance|gt:0 %}
                        (<span class="money positive">+{{ balance|floatformat:0 }}</span>)
                        {% else %}{% if balance|lt:0 %}
                        (<span class="money negative">{{ balance|floatformat:0 }}</span>)
                        {% endif %}
                    {% endif %}</span>
                    <strong class="money {% if account.has_normal_balance %}positive{% else %}negative{% endif %}">{{ account.normal_balance|rollup:"assets"|floatformat:0 }}</strong>
                    {% endwith %}
                </td>
            </tr>
            {% endfor %}
            <tr class="total">
                <td class="right">Total:</td>
                <td class="money total">{% rollup_total "%.0f" "assets" %}</td>
            </tr>
        </table>
    </div>

    <div id="expense-list" class="account-group">
    <div class="header">Expenses and Income: 
        <a href="{% url admin:accounts-add-expense %}">Add Expense</a> |
        <a href="{% url admin:accounts-add-revenue %}">Add Revenue</a>
    </div>
        <table>
            <tr>
                <th>Accounts</th>
                <th>{{ 5|get_months_back|date:"M" }}</th>
                <th>{{ 4|get_months_back|date:"M" }}</th>
                <th>{{ 3|get_months_back|date:"M" }}</th>
                <th>{{ 2|get_months_back|date:"M" }}</th>
                <th>{{ 1|get_months_back|date:"M" }}</th>
                <th>{{ 0|get_months_back|date:"M" }}</th>
                <th><strong>Avg</strong></th>
            </tr>
            {% for account, stats in income_accounts|accounts_dataset:6 %}
            {% if account %}
            <tr class="{% cycle 'even' 'odd' %}{% ifchanged account.type %} divider{% endifchanged %}">
                <td class="name">{{ account.name }}</td>
                {% for date, item in stats.items %}
                {% if not forloop.last %}
                    {% if item|gt:0 %}
                    <td class="amount positive money">{{ item|floatformat:0 }}</td>
                    {% else %}{% if item|lt:0 %}
                    <td class="amount negative money">{{ item|floatformat:0 }}</td>
                    {% else %}
                    <td class="amount zero money">{{ item|floatformat:0 }}</td>
                    {% endif %}{% endif %}
                {% else %}
                <td class="amount average money">{{ item|floatformat:0 }}</td>
                {% endif %}
                {% endfor %}
            </tr>
            {% else %}
            <tr class="totals">
                <td class="name">Totals:</td>
                {% for item in stats %}
                {% if not forloop.last %}
                    {% if item|gt:0 %}
                    <td class="amount positive money">{{ item|floatformat:0 }}</td>
                    {% else %}{% if item|lt:0 %}
                    <td class="amount negative money">{{ item|floatformat:0 }}</td>
                    {% else %}
                    <td class="amount zero money">{{ item|floatformat:0 }}</td>
                    {% endif %}
                    {% endif %}
                {% else %}
                <td class="amount average money">{{ item|floatformat:0 }}</td>
                {% endif %}
                {% endfor %}
            </tr>
            {% endif %}
            {% endfor %}
        </table>
    </div>
</div>

<div class="clear"></div>
{% endblock %}
